package com.example.spring.service;

import com.example.spring.entity.Record;
import com.example.spring.entity.Warehouse;
import com.example.spring.mapper.GoodMapper;
import com.example.spring.mapper.RecordMapper;
import com.example.spring.mapper.SlotMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Blob;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;



@Service
public class RecordService {
    @Autowired
    private RecordMapper recordMapper;

    @Autowired
    private GoodMapper goodMapper;

    @Autowired
    private SlotMapper slotMapper;

    public int save(Record record){
        if(record.getRid() == null) { //无id新增id
            return recordMapper.insert_data(record);
        }
        else {//否则更新
            return recordMapper.update(record);
        }
    }

    //查询某仓库某段时间内总数量变化情况
    public Map<String, Object> total_analyze(Integer whid, Timestamp minTime)
    {
        List<Record> record = recordMapper.findByMinTimeWhid(minTime, whid);
        Map<Date, Integer>change = new TreeMap<>();
        Iterator<Record> it = record.iterator();
        Integer totalNo = goodMapper.findByWhidTotal(whid);//当前仓库号为whid的货物总量
        while (it.hasNext())
        {
            Record record1 = it.next();
            Timestamp date = record1.getCreateTime();
            //如果没有key
            if (!change.containsKey(date))
            {
                //入库加一，出库减一
                if (record1.getEvent().equals("in"))
                {
                    change.put(date, --totalNo);
                }
                else if (record1.getEvent().equals("out"))
                {
                    change.put(date, ++totalNo);
                }
            }
            //如果有key
            else
            {
                if (record1.getEvent().equals("in"))
                {
                    Integer tmp = change.get(date);
                    change.put(date, --totalNo);
                }
                else if (record1.getEvent().equals("out"))
                {
                    Integer tmp = change.get(date);
                    change.put(date, ++totalNo);
                }
            }
        }
        List<String> result1 = new ArrayList(change.keySet());
        List<String> result2 = new ArrayList(change.values());
        Map<String, Object> map = new HashMap<>();
        map.put("1",result1);
        map.put("2",result2);
        return map;
    }


//以下是统计部分
    //某段时间内仓库内货物总数变化情况
    //按日期统计
    public Map<String, Object> total_analyze_date(Integer whid, Timestamp minTime,  Timestamp maxTime) throws ParseException
    {
        List<Record> record = recordMapper.findByMinTimeWhid(minTime, whid);
        Map<String, Integer>change = new TreeMap<>();
        Iterator<Record> it = record.iterator();
        Integer totalNo = goodMapper.findByWhidTotal(whid);//当前仓库号为whid的货物总量
        Integer endDateValue = totalNo;
        while (it.hasNext())
        {
            Record record1 = it.next();
            Timestamp time = record1.getCreateTime();
            String date = new SimpleDateFormat("yyyy-MM-dd").format(time);
            //入库加一，出库减一
            if (record1.getEvent().equals("in"))
            {
                --totalNo;
                if (time.before(maxTime))
                    change.put(date, totalNo);
                else
                {
                    endDateValue = totalNo;
                }
            }
            else if (record1.getEvent().equals("out"))
            {
                ++totalNo;
                if (time.before(maxTime))
                    change.put(date, totalNo);
                else
                {
                    endDateValue = totalNo;
                }
            }
        }
        String beginDateStr = new SimpleDateFormat("yyyy-MM-dd").format(minTime);
        String endDateStr = new SimpleDateFormat("yyyy-MM-dd").format(maxTime);
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = dateFormat1.parse(beginDateStr);
        Date endDate = dateFormat1.parse(endDateStr);
        Date date = endDate;
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        change.put(beginDateStr, totalNo);
        change.put(endDateStr, endDateValue);
        Integer tmp = 0;
        while (!date.equals(beginDate)) {
            String dateStr = dateFormat1.format(date);
            if(!change.containsKey(dateFormat1.format(date)))
            {
                change.put(dateStr, tmp);
            }
            else{
                tmp = change.get(dateStr);
            }
//            System.out.println(date);
            c.setTime(date);
            c.add(Calendar.DATE, -1); // 日期减1小时
            date = c.getTime();
        }
        List<String> result1 = new ArrayList(change.keySet());
        List<String> result2 = new ArrayList(change.values());
        Map<String, Object> map = new HashMap<>();
        map.put("time",result1);
        map.put("value",result2);
        return map;
    }
    //按小时查询一段时间内的
    public Map<String, Object> total_analyze_hour(Integer whid, Timestamp minTime, Timestamp maxTime) throws ParseException
    {
        List<Record> record = recordMapper.findByMinTimeWhid(minTime, whid);
        Map<String, Integer>change = new TreeMap<>();
        Iterator<Record> it = record.iterator();
        Integer totalNo = goodMapper.findByWhidTotal(whid);//当前仓库号为whid的货物总量
        Integer endDateValue = totalNo;
        while (it.hasNext())
        {
            Record record1 = it.next();
            Timestamp time = record1.getCreateTime();
            String date = new SimpleDateFormat("yyyy-MM-dd HH").format(time);
            //入库加一，出库减一
            if (record1.getEvent().equals("in"))
            {
                --totalNo;
                if (time.before(maxTime))
                    change.put(date, totalNo);
                else
                {
                    endDateValue = totalNo;
                }
            }
            else if (record1.getEvent().equals("out"))
            {
                ++totalNo;
                if (time.before(maxTime))
                    change.put(date, totalNo);
                else
                {
                    endDateValue = totalNo;
                }
            }
        }
        String beginDateStr = new SimpleDateFormat("yyyy-MM-dd HH").format(minTime);
        String endDateStr = new SimpleDateFormat("yyyy-MM-dd HH").format(maxTime);
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH");
        Date beginDate = dateFormat1.parse(beginDateStr);
        Date endDate = dateFormat1.parse(endDateStr);
        Date date = endDate;
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        change.put(beginDateStr, totalNo);
        change.put(endDateStr, endDateValue);
        Integer tmp = 0;
        while (!date.equals(beginDate)) {
            String dateStr = dateFormat1.format(date);
            if(!change.containsKey(dateFormat1.format(date)))
            {
                change.put(dateStr, tmp);
            }
            else{
                tmp = change.get(dateStr);
            }
//            System.out.println(date);
            c.setTime(date);
            c.add(Calendar.HOUR, -1); // 日期减1小时
            date = c.getTime();
        }
        List<String> result1 = new ArrayList(change.keySet());
        List<String> result2 = new ArrayList(change.values());
        Map<String, Object> map = new HashMap<>();
        map.put("time",result1);
        map.put("value",result2);
        return map;
    }


    //某段时间内，仓库内某类货物变化情况
    //按日期统计
    public Map<String, Object> type_analyze_date(Integer whid, String type, Timestamp minTime,  Timestamp maxTime) throws ParseException
    {
        List<Record> record = recordMapper.findByMinTimeTypeWhid(minTime, type, whid);
        Map<String, Integer>change = new TreeMap<>();
        Iterator<Record> it = record.iterator();
        Integer totalNo = goodMapper.findByType_Total(whid, type);//当前仓库号为whid，某type的货物总量
        Integer endDateValue = totalNo;
        while (it.hasNext())
        {
            Record record1 = it.next();
            Timestamp time = record1.getCreateTime();
            String date = new SimpleDateFormat("yyyy-MM-dd").format(time);
            //入库加一，出库减一
            if (record1.getEvent().equals("in"))
            {
                --totalNo;
                if (time.before(maxTime))
                    change.put(date, totalNo);
                else
                {
                    endDateValue = totalNo;
                }
            }
            else if (record1.getEvent().equals("out"))
            {
                ++totalNo;
                if (time.before(maxTime))
                    change.put(date, totalNo);
                else
                {
                    endDateValue = totalNo;
                }
            }
        }
        String beginDateStr = new SimpleDateFormat("yyyy-MM-dd").format(minTime);
        String endDateStr = new SimpleDateFormat("yyyy-MM-dd").format(maxTime);
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = dateFormat1.parse(beginDateStr);
        Date endDate = dateFormat1.parse(endDateStr);
        Date date = endDate;
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        change.put(beginDateStr, totalNo);
        change.put(endDateStr, endDateValue);
        Integer tmp = 0;
        while (!date.equals(beginDate)) {
            String dateStr = dateFormat1.format(date);
            if(!change.containsKey(dateFormat1.format(date)))
            {
                change.put(dateStr, tmp);
            }
            else{
                tmp = change.get(dateStr);
            }
//            System.out.println(date);
            c.setTime(date);
            c.add(Calendar.DATE, -1); // 日期减1小时
            date = c.getTime();
        }
        List<String> result1 = new ArrayList(change.keySet());
        List<String> result2 = new ArrayList(change.values());
        Map<String, Object> map = new HashMap<>();
        map.put("time",result1);
        map.put("value",result2);
        return map;
    }
    //按小时查询一段时间内的
    public Map<String, Object> type_analyze_hour(Integer whid, String type, Timestamp minTime, Timestamp maxTime) throws ParseException
    {
        List<Record> record = recordMapper.findByMinTimeTypeWhid(minTime, type, whid);
        Map<String, Integer>change = new TreeMap<>();
        Iterator<Record> it = record.iterator();
        Integer totalNo = goodMapper.findByType_Total(whid, type);//当前仓库号为whid，某type的货物总量
        Integer endDateValue = totalNo;
        while (it.hasNext())
        {
            Record record1 = it.next();
            Timestamp time = record1.getCreateTime();
            String date = new SimpleDateFormat("yyyy-MM-dd HH").format(time);
            //入库加一，出库减一
            if (record1.getEvent().equals("in"))
            {
                --totalNo;
                if (time.before(maxTime))
                    change.put(date, totalNo);
                else
                {
                    endDateValue = totalNo;
                }
            }
            else if (record1.getEvent().equals("out"))
            {
                ++totalNo;
                if (time.before(maxTime))
                    change.put(date, totalNo);
                else
                {
                    endDateValue = totalNo;
                }
            }
        }
        String beginDateStr = new SimpleDateFormat("yyyy-MM-dd HH").format(minTime);
        String endDateStr = new SimpleDateFormat("yyyy-MM-dd HH").format(maxTime);
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH");
        Date beginDate = dateFormat1.parse(beginDateStr);
        Date endDate = dateFormat1.parse(endDateStr);
        Date date = endDate;
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        change.put(beginDateStr, totalNo);
        change.put(endDateStr, endDateValue);
        Integer tmp = 0;
        while (!date.equals(beginDate)) {
            String dateStr = dateFormat1.format(date);
            if(!change.containsKey(dateFormat1.format(date)))
            {
                change.put(dateStr, tmp);
            }
            else{
                tmp = change.get(dateStr);
            }
//            System.out.println(date);
            c.setTime(date);
            c.add(Calendar.HOUR, -1); // 日期减1小时
            date = c.getTime();
        }
        List<String> result1 = new ArrayList(change.keySet());
        List<String> result2 = new ArrayList(change.values());
        Map<String, Object> map = new HashMap<>();
        map.put("time",result1);
        map.put("value",result2);
        return map;
    }


    //某段时间内，某个货架的货物的变化情况
    //按日期统计
    public Map<String, Object> slot_analyze_date (Integer sid, Timestamp minTime, Timestamp maxTime) throws ParseException
    {
        List<Record> record = recordMapper.findByMinTimeSlot(minTime, sid);
        Map<String, Integer>change = new TreeMap<>();
        Iterator<Record> it = record.iterator();
        Integer totalNo = goodMapper.findBySlot_Total(sid);//当前货架号为sid的货物总量
        Integer endDateValue = totalNo;
        while (it.hasNext())
        {
            Record record1 = it.next();
            Timestamp time = record1.getCreateTime();
            String date = new SimpleDateFormat("yyyy-MM-dd").format(time);
            //入库加一，出库减一
            if (record1.getEvent().equals("in"))
            {
                --totalNo;
                if (time.before(maxTime))
                    change.put(date, totalNo);
                else
                {
                    endDateValue = totalNo;
                }
            }
            else if (record1.getEvent().equals("out"))
            {
                ++totalNo;
                if (time.before(maxTime))
                    change.put(date, totalNo);
                else
                {
                    endDateValue = totalNo;
                }
            }
        }
        String beginDateStr = new SimpleDateFormat("yyyy-MM-dd").format(minTime);
        String endDateStr = new SimpleDateFormat("yyyy-MM-dd").format(maxTime);
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = dateFormat1.parse(beginDateStr);
        Date endDate = dateFormat1.parse(endDateStr);
        Date date = endDate;
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        change.put(beginDateStr, totalNo);
        change.put(endDateStr, endDateValue);
        Integer tmp = 0;
        while (!date.equals(beginDate)) {
            String dateStr = dateFormat1.format(date);
            if(!change.containsKey(dateFormat1.format(date)))
            {
                change.put(dateStr, tmp);
            }
            else{
                tmp = change.get(dateStr);
            }
//            System.out.println(date);
            c.setTime(date);
            c.add(Calendar.DATE, -1); // 日期减1天
            date = c.getTime();
        }
        List<String> result1 = new ArrayList(change.keySet());
        List<String> result2 = new ArrayList(change.values());
        Map<String, Object> map = new HashMap<>();
        map.put("time",result1);
        map.put("value",result2);
        return map;
    }
    //按小时查询一段时间内的
    public Map<String, Object> slot_analyze_hour(Integer sid, Timestamp minTime,  Timestamp maxTime) throws ParseException
    {
        List<Record> record = recordMapper.findByMinTimeSlot(minTime, sid);
        Map<String, Integer>change = new TreeMap<>();
        Iterator<Record> it = record.iterator();
        Integer totalNo = goodMapper.findBySlot_Total(sid);//当前货架号为sid的货物总量
        Integer endDateValue = totalNo;
        while (it.hasNext())
        {
            Record record1 = it.next();
            Timestamp time = record1.getCreateTime();
            String date = new SimpleDateFormat("yyyy-MM-dd HH").format(time);
            //入库加一，出库减一
            if (record1.getEvent().equals("in"))
            {
                --totalNo;
                if (time.before(maxTime))
                    change.put(date, totalNo);
                else
                {
                    endDateValue = totalNo;
                }
            }
            else if (record1.getEvent().equals("out"))
            {
                ++totalNo;
                if (time.before(maxTime))
                    change.put(date, totalNo);
                else
                {
                    endDateValue = totalNo;
                }
            }
        }
        String beginDateStr = new SimpleDateFormat("yyyy-MM-dd HH").format(minTime);
        String endDateStr = new SimpleDateFormat("yyyy-MM-dd HH").format(maxTime);
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH");
        Date beginDate = dateFormat1.parse(beginDateStr);
        Date endDate = dateFormat1.parse(endDateStr);
        Date date = endDate;
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        change.put(beginDateStr, totalNo);
        change.put(endDateStr, endDateValue);
        Integer tmp = 0;
        while (!date.equals(beginDate)) {
            String dateStr = dateFormat1.format(date);
            if(!change.containsKey(dateFormat1.format(date)))
            {
                change.put(dateStr, tmp);
            }
            else{
                tmp = change.get(dateStr);
            }
//            System.out.println(date);
            c.setTime(date);
            c.add(Calendar.HOUR, -1); // 日期减1小时
            date = c.getTime();
        }
        List<String> result1 = new ArrayList(change.keySet());
        List<String> result2 = new ArrayList(change.values());
        Map<String, Object> map = new HashMap<>();
        map.put("time",result1);
        map.put("value",result2);
        return map;
    }

    //某时刻，指定仓库的各类别货品的数量
    public Map<String, Integer> type_proportion_wh(Integer whid, Timestamp time)
    {
        List<String> types = recordMapper.find_history_types_wh(whid);
        Iterator<String> its = types.iterator();
        Map<String, Integer> map = new HashMap<>();
        while(its.hasNext()) {
            String type = its.next();
            List < Record > record = recordMapper.findByMinTimeTypeWhid(time, type, whid);
            Iterator<Record> itr = record.iterator();
            Integer totalNo = goodMapper.findByType_Total(whid, type);//当前仓库号为whid，某type的货物总量
            while (itr.hasNext()) {
                Record record1 = itr.next();
                //入库加一，出库减一
                if (record1.getEvent().equals("in")) {
                    --totalNo;
                } else if (record1.getEvent().equals("out")) {
                    ++totalNo;
                }
            }
            map.put(type, totalNo);
        }
        return map;
    }

    public Map<Integer, Object> slot_analyze_date_all(Integer whid, Timestamp minTime, Timestamp maxTime) {
        Map<Integer, Object> res = new TreeMap<>();
        List<Integer> slot = slotMapper.findWhSlot(whid);
        Iterator<Integer> it = slot.iterator();
        while(it.hasNext())
        {
            Integer tmp = it.next();
            try {
                res.put(tmp, this.slot_analyze_date(tmp, minTime, maxTime));
            }
            catch(ParseException e)
            {

            }
        }
        return res;
    }

    public Map<Integer, Object> slot_analyze_hour_all(Integer whid, Timestamp minTime, Timestamp maxTime) {
        Map<Integer, Object> res = new TreeMap<>();
        List<Integer> slot = slotMapper.findWhSlot(whid);
        Iterator<Integer> it = slot.iterator();
        while(it.hasNext())
        {
            Integer tmp = it.next();
            try {
                res.put(tmp, this.slot_analyze_hour(tmp, minTime, maxTime));
            }
            catch(ParseException e)
            {

            }
        }
        return res;
    }

    public Map<String, Object> type_analyze_date_all(Integer whid, Timestamp minTime, Timestamp maxTime) {
        Map<String, Object> res = new TreeMap<>();
        List<String> type = goodMapper.findWhType(whid);
        Iterator<String> it = type.iterator();
        while(it.hasNext())
        {
            String tmp = it.next();
            try{
                res.put(tmp, this.type_analyze_date(whid, tmp, minTime, maxTime));
            }
            catch(ParseException e)
            {

            }
        }
        return res;
    }

    public Map<String, Object> type_analyze_hour_all(Integer whid, Timestamp minTime, Timestamp maxTime) {
        Map<String, Object> res = new TreeMap<>();
        List<String> type = goodMapper.findWhType(whid);
        Iterator<String> it = type.iterator();
        while(it.hasNext())
        {
            String tmp = it.next();
            try{
                res.put(tmp, this.type_analyze_hour(whid, tmp, minTime, maxTime));
            }
            catch(ParseException e)
            {

            }
        }
        return res;
    }


//    //某时刻,指定slot各类货品的数量
//    public Map<String, Integer> type_proportion_slot(Integer sid, Timestamp minTime)
//    {
//        List<String> types = recordMapper.find_history_types_slot(sid);
//        Iterator<String> its = types.iterator();
//        Map<String, Integer> map = new HashMap<>();
//        while(its.hasNext()) {
//            String type = its.next();
//            List < Record > record = recordMapper.findByMinTimeSlot(minTime, sid);
//            Iterator<Record> itr = record.iterator();
//            Integer totalNo = goodMapper.findBySlot_Total(sid);//找到当前的slot里指定type的总数
//            while (itr.hasNext()) {
//                Record record1 = itr.next();
//                //入库加一，出库减一
//                if (record1.getEvent().equals("in")) {
//                    --totalNo;
//                } else if (record1.getEvent().equals("out")) {
//                    ++totalNo;
//                }
//            }
//            map.put(type, totalNo);
//        }
//        return map;
//    }

}
